package com.gk.study.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.gk.study.common.APIResponse;
import com.gk.study.common.CourseStatus;
import com.gk.study.common.ResponeCode;
import com.gk.study.entity.Course;
import com.gk.study.entity.Order;
import com.gk.study.service.CourseService;
import com.gk.study.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/teacher/order")
public class TeacherOrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    CourseService courseService;

    // 获取待处理的订单（状态为1）
    @GetMapping("/pending")
    public APIResponse getPendingOrders(String teacherId) {
        // 使用 LambdaQueryWrapper
        LambdaQueryWrapper<Order> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Order::getThingId, teacherId)
                .eq(Order::getStatus, "1");
        List<Order> orders = orderService.list(queryWrapper);
        return new APIResponse(ResponeCode.SUCCESS, "查询成功", orders);
    }

    // 教师接受订单
    @PostMapping("/accept")
    public APIResponse acceptOrder(@RequestParam Long orderId) {
        Order order = orderService.getById(orderId);
        order.setStatus("2"); // 假设 "2" 是已确认状态
        orderService.updateById(order);

        // 更新课程状态为进行中
        LambdaQueryWrapper<Course> query = new LambdaQueryWrapper<>();
        query.eq(Course::getOrderId, orderId);
        Course course = courseService.getOne(query);
        if (course != null) {
            course.setStatus(CourseStatus.PROGRESS);
            courseService.updateById(course);
        }

        return APIResponse.success("已接受订单");
    }

    // 教师拒绝订单
    @PostMapping("/reject")
    public APIResponse rejectOrder(Long orderId) {
        Order order = new Order();
        order.setId(orderId);
        order.setStatus("5"); // 5=已拒绝
        orderService.updateById(order);
        return new APIResponse(ResponeCode.SUCCESS, "已拒绝订单");
    }
}